<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    var a = [1, 3, 6, 3, 23, 76, 1, 34, 222, 6, 456, 221];

    function mergeSort(array) {
      const merge = (right, left) => {
        const result = [];
        let il = 0,
          ir = 0;
        while (il < left.length && ir < right.length) {
          if (left[il] < right[ir]) {
            result.push(left[il++]);
          } else {
            result.push(right[ir++]);
          }
        }
        while (il < left.length) {
          result.push(left[il++]);
        }
        while (ir < right.length) {
          result.push(right[ir++]);
        }
        return result;
      };
      const mergeSort = (array) => {
        if (array.length === 1) {
          return array;
        }
        const mid = Math.floor(array.length / 2);
        const left = array.slice(0, mid);
        const right = array.slice(mid, array.length);
        return merge(mergeSort(left), mergeSort(right));
      };
      return mergeSort(array);
    }
    console.log(mergeSort(a));
  </script>
</html>
